package com.njupt.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;

import com.njupt.dao.BaseDao;
import com.njupt.dao.PaginationSupport;
import com.njupt.service.UniversalManager;


public class UniversalManagerImpl implements UniversalManager {
    /**
     * Log instance for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
     */
    protected final Log log = LogFactory.getLog(getClass());

    /**
     * UniversalDao instance, ready to charge forward and persist to the database
     */
    protected BaseDao dao;
 
    public void setDao(BaseDao dao) {
        this.dao = dao;
    }

    /**
     * {@inheritDoc}
     */
    public Object get(Class clazz, Serializable id) {
        return dao.get(clazz, id);
    }

    /**
     * {@inheritDoc}
     */
    public List getAll(Class clazz) {
        return dao.getAll(clazz);
    }

    /**
     * {@inheritDoc}
     */
    public void remove(Class clazz, Serializable id) {
        dao.remove(clazz, id);
    }

    /**
     * {@inheritDoc}
     */
    public Object save(Object o) {
        return dao.save(o);
    }
    
    public List findByExample(Object o){
    	return dao.findByExample(o);
    }

    public List findByHql(String hql){
    	return dao.executeQuery(hql);
    }
    public List findByHql(String hql, Object[] values){
    	return dao.executeQuery(hql,values);
    }
    public List findByHql(String hql, Object value){
    	return dao.executeQuery(hql,new Object[]{value});
    }
    public List find(String hql){
    	return dao.executeQuery(hql);
    }
    public List find(String hql, Object[] values){
    	return dao.executeQuery(hql,values);
    }
    public List find(String hql, Object value){
    	return dao.executeQuery(hql,new Object[]{value});
    }
	public int executeUpdate(String query) {
		return dao.executeUpdate(query);
	}
    
	public int executeUpdate(String query, Object value){
		return dao.executeUpdate(query,value);
	}
	
	public int executeUpdate(String query, Object[] values){
		return dao.executeUpdate(query,values);
	}

	public <T> Criteria createCriteria(Class<T> entityClass, Criterion... criterions) {
		return dao.createCriteria(entityClass, criterions);
	}

	public <T> T findUniqueBy(Class<T> entityClass, String propertyName, Object value) {
		return (T) dao.findUniqueBy(entityClass, propertyName, value);
	}
	
	public List findAllByCriteria(DetachedCriteria detachedCriteria) {
		return dao.findByCriteria(detachedCriteria);
	}

	public PaginationSupport findByExample(Object example, int start) {
		return dao.findByExample( example,  start);
	}


	public PaginationSupport findPageByCriteria(
			DetachedCriteria detachedCriteria) {
		return dao.findPageByCriteria(detachedCriteria,0,PaginationSupport.PAGESIZE);
	}


	public PaginationSupport findPageByCriteria(
			DetachedCriteria detachedCriteria, int startIndex) {
		return dao.findPageByCriteria(
				 detachedCriteria,  startIndex);
	}


	public PaginationSupport findPageByCriteria(
			DetachedCriteria detachedCriteria, int startIndex, int pageSize) {
		return dao.findPageByCriteria(
				 detachedCriteria,  startIndex,  pageSize);
	}


	public PaginationSupport findPageByHql(String hql, int startIndex) {
		return dao.findPageByHql( hql, startIndex, PaginationSupport.PAGESIZE, null);
	}


	public PaginationSupport findPageByHql(String hql, int startIndex,
			int pageSize) {
		return dao.findPageByHql( hql,  startIndex,
				 pageSize);
	}


	public PaginationSupport findPageByHql(String hql, int startIndex,
			Object[] values) {
		return dao.findPageByHql( hql,  startIndex, PaginationSupport.PAGESIZE,
				 values) ;
	}


	public PaginationSupport findPageByHql(String hql, int startIndex,
			int pageSize, Object[] values) {
		return dao.findPageByHql( hql,  startIndex,
				 pageSize,  values);
	}


	public PaginationSupport findPageByHql(String hql, String countHql,
			int startIndex, Object[] values) {
		return dao.findPageByHql( hql,  countHql,
				 startIndex, PaginationSupport.PAGESIZE, values);
	}


	public PaginationSupport findPageByHql(String hql, String countHql,
			int startIndex, int pageSize, Object[] values) {
		return dao.findPageByHql( hql,  countHql,
				 startIndex,  pageSize,  values);
	}
	/**
	 * 
	 * @param dc
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public PaginationSupport paginateByCriteria(DetachedCriteria dc, int pageNo, int pageSize){
		int startIndex = (pageNo - 1) * pageSize;
		return this.findPageByCriteria(dc, startIndex, pageSize);
	}


	public int getCountByCriteria(DetachedCriteria detachedCriteria) {
		return dao.getCountByCriteria(detachedCriteria);
	}

	public List findByNativeSql(String nativeSql, Class[] entities,
			Object[] params) {
		return dao.findByNativeSql(nativeSql, entities, params);
	}

	public PaginationSupport paginateByNativeSql(String nativeSql,
			Class[] entities, Object[] params, int startIndex, int pageSize) {
		return dao.paginateByNativeSql(nativeSql,entities, params, startIndex, pageSize);
	}

	public PaginationSupport paginateByNativeSql(String nativeSql,
			Object[] params, int startIndex, int pageSize) {
		return dao.paginateByNativeSql(nativeSql,null, params, startIndex, pageSize);
	}

	public PaginationSupport paginateByNativeSql(String nativeSql,
			Class entityType, Object[] params, int startIndex, int pageSize) {
		return dao.paginateByNativeSql(nativeSql,new Class[]{entityType}, params, startIndex, pageSize);
	}
	public PaginationSupport paginateAsMapByNativeSql(String nativeSql,
			Object[] params, int startIndex, int pageSize) {
		return dao.paginateAsMapByNativeSql(nativeSql, params, startIndex, pageSize);
	}

	public Object queryUniqueResult(String hql, Object... params) {
		return dao.queryForUnique(hql, params);
	}

	public Object queryUniqueResult(DetachedCriteria detachedCriteria) {
		return dao.queryUniqueResultByCriteria(detachedCriteria);
	}

	public List findByNativeSql(String nativeSql, String[] alias,
			Class[] entityTypes, Object[] params) {
		return dao.findByNativeSql(nativeSql, alias, entityTypes, params);
	}

	public PaginationSupport paginateByNativeSql(String nativeSql,
			String[] alias, Class[] entityTypes, Object[] params, int pageNo,
			int pageSize) {
		int startIndex = (pageNo-1)*pageSize;
		return dao.paginateByNativeSql(nativeSql, alias, entityTypes, params, startIndex, pageSize);
	}
	public PaginationSupport paginateByHql(String hql, int pageNo,
			int pageSize, Object... params) {
		int startIndex = pageNo*pageSize-pageSize;
		return findPageByHql(hql, startIndex, pageSize, params);
	}
	/**
	 * 执行修改数据的sql。
	 * @param nativeSql
	 * @param params
	 * @return
	 */
	public int executeUpdateByNativeSql(final String nativeSql,final Object[] params){
		return dao.executeUpdateByNativeSql(nativeSql,params);
	}

	public List findByNativeSqlAsMap(String nativeSql, Object[] params) {
		return dao.findByNativeSqlAsMap(nativeSql, params);
	}
}

